package com.attcatstudy.Thread;/**
 *
 * @author attcat
 * ---做一个有趣的程序员
 * @date  2022-10-31 21:03
 * @description  优雅的停止线程
 *
 */public class StopThread implements Runnable{


        public static void main(String[] args) throws InterruptedException {
             Thread thread = new Thread(new StopThread());
             thread.start();
           Thread.sleep(5);
             thread.interrupt();
    }

    @Override
    public void run() {
        int count = 0;
        while (!Thread.currentThread().isInterrupted() && count < 1000) {
             System.out.println("count = " + count++);
            try {
               Thread.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
         }
        System.out.println("线程停止: stop thread");
    }
}
